package com.hengpeng.msls.service.userInfo.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.en.TransType;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.userInfo.MobileBingRequest;
import com.hengpeng.itfinbase.message.userInfo.SendEmailRequest;
import com.hengpeng.itfinbase.message.userInfo.SendSMSRequest;
import com.hengpeng.itfinbase.message.userInfo.UserInfoResponse;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinbase.persist.VerifyCode.VerifyType;
import com.hengpeng.msls.port.client.vo.client.AccountLog;
import com.hengpeng.msls.port.client.vo.client.Passport;
import com.hengpeng.msls.service.BaseService;
import com.hengpeng.msls.service.userInfo.UserInfoService;
import com.hengpeng.msls.util.CommUtil;

/**
 * 用户
 * @author wuhaifei
 * @version 1.0, 2012-5-22
 */
@Service
public class UserInfoServiceImpl extends BaseService implements UserInfoService
{
	private final static Log logger = LogFactory.getLog(UserInfoServiceImpl.class);

	private Map<String, Passport> usersToken = new HashMap<String, Passport>();

	@Override
	public UserInfo getUserInfo(String userName)
	{
		DynamicQuery query = new DynamicQuery(UserInfo.class);
		query.eq("userName", userName);
		return serviceFactory.getProtFactory().getEntity(query, UserInfo.class);
	}

	@Override
	public List<UserInfo> getUserInfoByMone(String mobile)
	{
		DynamicQuery query = new DynamicQuery(UserInfo.class);
		query.eq("mobile", mobile);
		return serviceFactory.getProtFactory().queryList(query, UserInfo.class);
	}

	@Override
	public UserInfoResponse shortcutLogin(Map<String, String> oprateParams)
	{
		return null;
	}

	@Override
	public UserInfoResponse doLogin(String userName, String betCardNo, String password, String betPwd, String storeId,
			String ipAddress)
	{
		return null;
	}

	@Override
	public UserInfoResponse doRegist(String userName, String password, String storeId)
	{
		return null;
	}

	@Override
	public Passport getPassprot(String userName)
	{
		return null;
	}

	/**
	 * 短信验证发送
	 * @param mobile
	 * @param username
	 * @param content
	 * @return
	 */
	@Override
	public ResponseMessage sendSMSForValidMobile(String username, String mobile, String content, VerifyType type)
	{
		SendSMSRequest smsRequest = new SendSMSRequest();
		smsRequest.setUsername(username);
		smsRequest.setMobile(mobile);
		smsRequest.setType(type);
		smsRequest.setContent(content);
		smsRequest.setTransType(TransCodeConstants.MemberTransCode.MEMBER_VALID_MOBILE);
		return (ResponseMessage) serviceFactory.getProtFactory().remoteTrans(smsRequest);
	}

	/**
	 * 查询资金明细
	 * @param userName 用户名
	 * @param beginTransTime 开始时间
	 * @param endTransTime结束时间
	 * @param transType 交易类型
	 * @param pageIndex 页码
	 * @param count 查询记录数
	 * @return
	 */
	@Override
	public QueryResult<AccountLog> findAccountDetail(String userName, Date beginTransTime, Date endTransTime,
			TransType[] transType, int pageIndex, int count)
	{
		QueryResult<AccountLog> result = new QueryResult<AccountLog>();
		if (CommUtil.isEmpty(userName))
		{
			result.setCode(RespCodeConstants.CommonCode.TRANS_FAIL);
			result.setMessage("用户名为空");
			return result;
		}
		pageIndex = (pageIndex <= 0 ? 1 : pageIndex);
		count = (count <= 0 ? 10 : count);
		try
		{
			List<com.hengpeng.itfinbase.persist.AccountLog> list = serviceFactory.getAccountService()
					.findAccountLogWithPage(userName, beginTransTime, endTransTime, transType, pageIndex, count);
			Long totalCount = serviceFactory.getAccountService().findAccountLogWithCount(userName, beginTransTime,
					endTransTime, transType);
			result.setCode(RespCodeConstants.CommonCode.TRANS_SUC);
			result.setMessage(RespCodeConstants.CommonCode.TRANS_SUC_MSG);
			result.setTotalCount(totalCount);
			result.setResults(conversAccounLogListToListVo(list));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			result.setCode(RespCodeConstants.CommonCode.TRANS_FAIL);
			result.setMessage("查询数据出错");
		}
		return result;
	}

	/**
	 * <pre>
	 * 手机号码绑定确定
	 * </pre>
	 * @param username
	 * @param mobile
	 * @param code
	 * @param type
	 * @return
	 */
	@Override
	public ResponseMessage doValidMobile(String username, String mobile, String code, VerifyType type)
	{
		SendSMSRequest request = new SendSMSRequest();
		request.setUsername(username);
		request.setMobile(mobile);
		request.setVerifyCode(code);
		request.setType(type);
		request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_VALID_MOBILE_AFFIRM);
		return (ResponseMessage) serviceFactory.getProtFactory().remoteTrans(request);
	}

	/**
	 * 邮箱验证发送邮件
	 * @param email
	 * @param username
	 * @param betcardno
	 * @param storeId
	 * @param url
	 * @param returnUrl
	 * @return
	 */
	@Override
	public ResponseMessage sendMailForValid(String email, String username, String url, String returnUrl)
	{
		SendEmailRequest request = new SendEmailRequest();
		request.setUsername(username);
		request.setEmail(email);
		request.setType(VerifyType.VALIDEMAIL);
		request.setUrl(url);
		request.setUsername(username);
		request.setReturnUrl(returnUrl);
		request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_VALID_EMAIL);
		return (ResponseMessage) serviceFactory.getProtFactory().remoteTrans(request);
	}

	/**
	 * 邮箱验证邮件确认
	 * @param username
	 * @param verifyCode
	 * @param email
	 * @return
	 */
	@Override
	public ResponseMessage doValidEmail(String username, String verifyCode, String email)
	{
		SendEmailRequest request = new SendEmailRequest();
		request.setUsername(username);
		request.setVerifyCode(verifyCode);
		request.setEmail(email);
		request.setType(VerifyType.VALIDEMAIL);
		request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_VALID_EMAIL_AFFIRM);
		return (ResponseMessage) serviceFactory.getProtFactory().remoteTrans(request);
	}

	/**
	 * <pre>
	 * 绑定手机号码
	 * </pre>
	 * @param request
	 * @return
	 */
	@Override
	public ResponseMessage bingMobile(String userName, String mobile)
	{
		MobileBingRequest request = new MobileBingRequest();
		request.setMobile(mobile);
		request.setUserName(userName);
		request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_BING_MOBILE);
		return (ResponseMessage) serviceFactory.getProtFactory().remoteTrans(request);
	}
}
